package com.jinyu.jyly.ui.mine.make_code;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.google.gson.Gson;
import com.jinyu.jyly.global.GlobalConstants;
import com.jinyu.jyly.utils.SharedPreferencesUtils;
import com.king.image.imageviewer.ImageViewer;
import com.king.image.imageviewer.loader.GlideImageLoader;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseLazyFragment;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.PayMentTypeInfo;
import com.jinyu.jyly.ui.mine.make_code.change.ChangeWeiChatCodeActivity;
import com.jinyu.jyly.utils.AES;
import com.qiniu.android.storage.Configuration;
import com.qiniu.android.storage.UploadManager;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 收款码
 *
 * @author 62485
 */
public class MakeCodeWeiChatTabFragment extends BaseLazyFragment<MakeCodePresenter> implements MakeCodeContract.View {


    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.tv_bank_e)
    TextView tvBankE;
    @BindView(R.id.iv_code)
    ImageView ivCode;
    @BindView(R.id.btn_sub)
    Button btnSub;
    private MakeCodePresenter mPresenter;
    private String wei_pic;


    public static MakeCodeWeiChatTabFragment newInstance(String type) {
        MakeCodeWeiChatTabFragment newFragment = new MakeCodeWeiChatTabFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        newFragment.setArguments(bundle);
        return newFragment;
    }

    @Override
    protected View initView(ViewGroup container) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.fragment_make_code_weichat, container, false);
        return view;
    }

    @Override
    protected void loadData() {

        mTipDialog.show();
        mPresenter.getPayMent();

    }

    @Override
    public MakeCodePresenter getPresenter() {
        if (mPresenter == null) {
            mPresenter = new MakeCodePresenter(getActivity(), this);
        }
        return mPresenter;
    }



    @Override
    public void onFailer(Throwable throwable) {
        mTipDialog.dismiss();
    }

    /**
     * 获取支付参数
     *
     * @param infoBean
     */
    @Override
    public void onPayMent(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        switch (infoBean.getStatus()) {
            case 200:
                String beanData = infoBean.getData();
                String result = null;
                try {
                    result = AES.decrypt(beanData);
                    Gson gson = new Gson();
                    PayMentTypeInfo paymentInfo = gson.fromJson(result, PayMentTypeInfo.class);
                    if (paymentInfo != null) {
                        tvBankName.setText(paymentInfo.getWei_num());
                        wei_pic = paymentInfo.getWei_pic();
                        RequestOptions options = new RequestOptions();
                        options.error(R.mipmap.space_list);
                        Glide.with(mContext).load(paymentInfo.getWei_pic())
                                .apply(options)
                                .into((ImageView) ivCode);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }
    }

    @OnClick({R.id.iv_code, R.id.btn_sub})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_code:
                ImageViewer.load(wei_pic)//要加载的图片数据，单张或多张
                        .imageLoader(new GlideImageLoader())//加载器，imageLoader必须配置，目前内置的有GlideImageLoader或PicassoImageLoader，也可以自己实现
                        .start(getActivity(),ivCode);//activity or fragment, 跳转时的共享元素视图
                break;
            case R.id.btn_sub:
                Intent mIntent = new Intent(getActivity(), ChangeWeiChatCodeActivity.class);
                startActivity(mIntent);
                break;
        }
    }


//    @OnClick(R.id.btn_sub)
//    public void onViewClicked() {
//
//    }
}
